package com.ncp.model.biz.cart;

import java.util.ArrayList;
import java.util.List;

import com.ncp.model.dao.DB;
import com.ncp.model.dao.DbConnection;
import com.ncp.model.entity.cart.OrderBasicBean;
import com.ncp.model.entity.order.OrderBasic;
import com.ncp.model.entity.order.OrderDetail;

public class HandleOrderBiz {
	private DB common =new DB();
	
	public int saveOrderBasic(OrderBasic orderBasicBean){
		int result = 0;
		String sql = "insert into ORDERBASIC(orderID,userID,orderTime,paymentType,totalprice,orderStatus,logisticsType,logisticsPayment,tradeID,buyerAccount,consignmentTime,expressID,receiverID)" +
				" values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
		common.setSql(sql);
        common.setConn(DbConnection.getConnByDataSource());
        List<Object> list = new ArrayList<Object>();
        list.add(0, orderBasicBean.getOrderID());
        list.add(1, orderBasicBean.getUserID());
        list.add(2, orderBasicBean.getOrderTime());
        list.add(3, orderBasicBean.getPaymentType());
        list.add(4, orderBasicBean.getTotalPrice());
        list.add(5, orderBasicBean.getOrderStatus());
        
        list.add(6, orderBasicBean.getLogisticsType());
        list.add(7, orderBasicBean.getLogisticsPayment());
        list.add(8, orderBasicBean.getTradeID());
        list.add(9, orderBasicBean.getBuyerAccount());
        list.add(10, orderBasicBean.getConsignmentTime());
        list.add(11, orderBasicBean.getExpressID());
        list.add(12, orderBasicBean.getReceiverID());
        common.setList(list);
        result = common.executeUpdate();
        return result;
	}
	
	public int saveOrderDetail(String orderID, OrderDetail orderDetailBean){
		int result = 0;
		String sql = "insert into ORDERDETAIL(OrderID,ProductID,ProductPrice,ProductNumber) values (?,?,?,?)";
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		List<Object> list = new ArrayList<Object>();
		list.add(0, orderID);
		list.add(1, orderDetailBean.getProductID());
		list.add(2, orderDetailBean.getProductPrice());
		list.add(3, orderDetailBean.getProductNumber());
        common.setList(list);
        result = common.executeUpdate();
		return result;
	}
	
	public int reduceProductNumber(int productID, int productNumber){
		int result = 0;
		String sql = "update PRODUCTBASIC set SoldPNumber = SoldPNumber + ? where ProductID = ?";
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		List<Object> list = new ArrayList<Object>();
		list.add(0, productNumber);
		list.add(1, productID);
		common.setList(list);
		result = common.executeUpdate();
		return result;
	}
	
}
